package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkCustomTemplet

import com.lancoo.cpbase.authentication.base.CurrentUser
import com.lancoo.znbkxx.teacher.Ext
import org.json.JSONException
import java.util.*

object jsonHelp {

    @Throws(JSONException::class)
    fun getLastCustomQues(
            typeNo: String,
            typeName: String,
            genreId: Int,
            genreName: String,
            quesCount: Int,
            itemCount: Int,
            singleEachScoreList: List<Double>,
            specialFeatureCodesList: List<String>
    ): MutableMap<String, Any> {

        val quesList: MutableList<Map<*, *>> = ArrayList()
        for (i in 0 until quesCount) {

            val singQuesList: MutableMap<String, Any> = HashMap()
            singQuesList["ItemNum"] = itemCount
            singQuesList["ScoreList"] = singleEachScoreList
            singQuesList["NewScoreList"] = singleEachScoreList

            quesList.add(singQuesList)
        }

        val customQuesInfo: MutableMap<String, Any> = HashMap()
        customQuesInfo["TypeNo"] = typeNo
        customQuesInfo["TypeName"] = typeName
        customQuesInfo["GenreID"] = genreId
        customQuesInfo["GenreName"] = genreName
        customQuesInfo["OSFlag"] = ""
        customQuesInfo["SpecialFeatureCodes"] = specialFeatureCodesList
        customQuesInfo["QuesList"] = quesList

        return customQuesInfo
    }

    //组装自定义模板单个题型的组卷模板
    @Throws(JSONException::class)
    fun getSingleCustomQues(
            typeNo: String,
            typeName: String,
            genreId: Int,
            genreName: String,
            quesCount: Int,
            itemCount: Int,
            eachScore: Double,
            specialFeatureCodes: String
    ): MutableMap<String, Any> {

        val quesList: MutableList<Map<*, *>> = ArrayList()
        for (i in 0 until quesCount) {

            val singleEachScoreList = ArrayList<String>()
            for (j in 0 until itemCount) {
                singleEachScoreList.add(eachScore.toString())
            }

            val singQuesList: MutableMap<String, Any> = HashMap()
            singQuesList["ItemNum"] = itemCount
            singQuesList["ScoreList"] = singleEachScoreList
            singQuesList["NewScoreList"] = singleEachScoreList

            quesList.add(singQuesList)
        }

        val specialFeatureCodesList = ArrayList<String>()
        if (specialFeatureCodes.isNotEmpty()) {
            specialFeatureCodesList.add(specialFeatureCodes)
        }

        val customQuesInfo: MutableMap<String, Any> = HashMap()
        customQuesInfo["TypeNo"] = typeNo
        customQuesInfo["TypeName"] = typeName
        customQuesInfo["GenreID"] = genreId
        customQuesInfo["GenreName"] = genreName
        customQuesInfo["OSFlag"] = ""
        customQuesInfo["SpecialFeatureCodes"] = specialFeatureCodesList
        customQuesInfo["QuesList"] = quesList

        return customQuesInfo
    }

    //组装整份自定义试卷模板
    @Throws(JSONException::class)
    fun getCustomTemplet(
            customTemplet: CustomTemplet?,
            contentSize: Int,
            addCustomQuesInfor: CustomQuesTypeInfor,
            state: Int,
            templetType: Int,
            answerLength: Int,
            templetName: String,
            templetId: String
    ): MutableMap<String, Any> {

        if (Ext.GLOBAL_GRADE_STU == "K6" || Ext.GLOBAL_GRADE_STU == "K7" || Ext.GLOBAL_GRADE_STU == "K8" || Ext.GLOBAL_GRADE_STU == "K9") {
            Ext.STAGE_NO = "B"
        } else if (Ext.GLOBAL_GRADE_STU == "K10" || Ext.GLOBAL_GRADE_STU == "K11" || Ext.GLOBAL_GRADE_STU == "K12") {
            Ext.STAGE_NO = "C"
        }

        val jsonObjects: MutableMap<String, Any> = HashMap()
        jsonObjects["TID"] = CurrentUser.UserID
        jsonObjects["CourseClassID"] = Ext.COURSE_CLASSID
        jsonObjects["SubjectID"] = "C"
        jsonObjects["CountyID"] = CurrentUser.CountyID
        jsonObjects["ProvinceID"] = Ext.PROVINCE_ID
        jsonObjects["CityID"] = Ext.CITY_ID
        jsonObjects["State"] = state
        jsonObjects["TempletType"] = templetType
        jsonObjects["GlobalGrade"] = Ext.GLOBAL_GRADE_STU
        jsonObjects["AnswerLength"] = answerLength
        jsonObjects["SchoolID"] = Ext.SCHOOLID
        jsonObjects["TempletName"] = templetName
        jsonObjects["Token"] = CurrentUser.Token
        jsonObjects["UserID"] = CurrentUser.UserID
        jsonObjects["StageNo"] = Ext.STAGE_NO
        jsonObjects["TempletID"] = templetId

        val contentList: MutableList<CustomQuesTypeInfor> = ArrayList()
        for (i in 0 until contentSize) {
            if (i == contentSize - 1) {
                contentList.add(addCustomQuesInfor)
            } else {
                contentList.add(customTemplet!!.Content[i])
            }
        }
        jsonObjects["Content"] = contentList
//        return Gson().toJson(jsonObjects)  //String
        return jsonObjects
    }

}